import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.webservice.service.RmGetDataApiService;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.util.*;

public class Base64 {
//    public static void main(String[] args) throws UnsupportedEncodingException {
//        final org.apache.commons.codec.binary.Base64 base64 = new org.apache.commons.codec.binary.Base64();
//        String text = "PHJvb3Q+PGhlYWQ+PEg+CjxTVEFSVFRJTUU+MjAxODA0MjcwMDAyPC9TVEFSVFRJTUU+CjxFTkRUSU1FPjIwMTgwNTI3MDAwMjwvRU5EVElNRT4KPE9SR19JRD44MTwvT1JHX0lEPgo8L0g+PC9oZWFkPjwvcm9vdD4=";
//        String text2 = "PHJvb3Q+PGhlYWQ+PEg+CjxTVEFSVFRJTUU+MjAxODA0MjcwMDAyPC9TVEFSVFRJTUU+CjxFTkRUSU1FPjIwMTgwNTI3MDAwMjwvRU5EVElNRT4KPE9SR19JRD44MTwvT1JHX0lEPgo8L0g+PC9oZWFkPjwvcm9vdD4=";
//        String a = new String(base64.decode(text), "UTF-8");
//        String b = new  String(base64.decode(text2), "UTF-8");
//        System.out.println("text : \n" + a);
//        System.out.println("text2 : \n" + b);
//        System.out.println(a.equals(b));
//    }

    @Test
    public void test() throws UnsupportedEncodingException {
        java.util.Base64.Encoder encoder = java.util.Base64.getEncoder();
        String text = "<root><head><H>\n" +
                "<START_TIME>201804270002</STARTTIME>\n" +
                "<ENDTIME>201805270002</ENDTIME>\n" +
                "<ORG_ID>81</ORG_ID>\n" +
                "</H></head></root>";
        byte[] textByte = text.getBytes("UTF-8");
        //编码
        String encodedText = encoder.encodeToString(textByte);
        String textCompare = "PHJvb3Q+PGhlYWQ+PEg+CjxTVEFSVFRJTUU+MjAxODA0MjcwMDAyPC9TVEFSVFRJTUU+CjxFTkRUSU1FPjIwMTgwNTI3MDAwMjwvRU5EVElNRT4KPE9SR19JRD44MTwvT1JHX0lEPgo8L0g+PC9oZWFkPjwvcm9vdD4=";
        System.out.println(encodedText);
        System.out.println(encodedText.equals(textCompare));
    }

    @Test
    public void test11 (){
        String a = "结果";
        String result = "{\"result\":\""+ a + "\"}";
        JSONObject json = JSON.parseObject(result);
        System.out.println(json.get("result").toString());
    }

    @Test
    public void test2 () throws UnsupportedEncodingException {
        RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();
        String docType = "CUX_PO_VENDOR_Q";
        Map<String, String> map = new HashMap<String, String>();
        map.put("STARTTIME","201804270002");
        map.put("ENDTIME","201805270002");
        map.put("ORG_ID","81");
        String encodeText = rmGetDataApiService.toBase64Entity(map);
        System.out.println(encodeText);
    }

    @Test
    public void test3(){
        final org.apache.commons.codec.binary.Base64 base64 = new org.apache.commons.codec.binary.Base64();
        String text ="PHJvb3Q+PGhlYWQ+PEg+PE1UUkxfTk8+MTI2MDAxMDAwMDAxNjwvTVRSTF9OTz48T1JHX0lEPjEwMjwvT1JHX0lEPjxBU05fTElORV9JRD4xPC9BU05fTElORV9JRD48UVVBTlRJVFk+NTAwPC9RVUFOVElUWT48UE9fTElORV9MT0NBVElPTl9JRD48L1BPX0xJTkVfTE9DQVRJT05fSUQ+PE1UUkxfSUQ+NDc1Mjg8L01UUkxfSUQ+PFBPX0xJTkU+MjwvUE9fTElORT48U1JfVFlQRT5QT1IsUE9JPC9TUl9UWVBFPjxBU05fSEVBUkRFUl9JRD5BU04tNTAwNTc0LTIwMDctMDAxPC9BU05fSEVBUkRFUl9JRD48UVVFVUVfSUQ+VzQ3MzUyMzExMDIwMTg1NjAwMDwvUVVFVUVfSUQ+PFRSWF9EQVRFPkZyaSBKdWwgMzEgMDA6MDg6MzEgQ1NUIDIwMjA8L1RSWF9EQVRFPjxNVU5JVD5udWxsPC9NVU5JVD48UE9fSUQ+MTAxMjAwNzAwMTA4PC9QT19JRD48U1RPUkVfSUQ+bnVsbDwvU1RPUkVfSUQ+PFNVUF9JRD40MTwvU1VQX0lEPjxTT1VSQ0VfTlVNPkJTVFIxMDIyMDA3MzAwMDA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gxODAwMDAwMDA2PC9TT1VSQ0VfTlVNPjxSRU1BUks+PC9SRU1BUks+PFRSQU5TQUNUSU9OX0lEPjQ4MDMwNDgwMTMyNzE1NzI0ODwvVFJBTlNBQ1RJT05fSUQ+PExPVF9OTz48L0xPVF9OTz48UE9fTElORV9JRD48L1BPX0xJTkVfSUQ+PC9IPjwvaGVhZD48L3Jvb3Q+";
        String a = new String(base64.decode(test5));
        System.out.println(a);

    }

    @Test
    public void test4(){
        String result = "";
        String senten = "abc sab df";
        String no = "df";
        String yes = "abcd";
        String[] a =  senten.split(" ");
        for (int i = 0 ; i < a.length; i++){
            if (a[i].equals(no)){
                a[i] = yes;
            }
        }
        for (int i = 0 ; i < a.length; i++){
            result = result + a[i] + " ";
        }
        System.out.println(result);
    }

    public static void main(String[] args)
    {
        String sentence, preWord, afterWord, output = "";
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter a sentence: ");
        sentence = input.nextLine();
        System.out.println("Please enter the string for replacement:");
        preWord = input.nextLine();
        System.out.println("Please enter the string you like to replace with:");
        afterWord = input.nextLine();
        String[] str = sentence.split(" ");
        for(int index = 0; index < str.length; index++)
        {
            String word = str[index];
            while(word.length() == preWord.length())
            {
                if(preWord.equals(word))
                {
                    sentence = sentence.replace(word, afterWord);
                    break;
                }
            }
        }
        System.out.println("After replacement, the sentence becomes: "  + sentence + ".");
    }

    @Test
    public void testSum(){
        int [] nums = {0,0,0,0};
        int target = 0;
        System.out.println(fourSum(nums, target));
    }

    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        for(int i = 0; i < nums.length -3; i++){
            for(int j = i+1; j < nums.length -2; j++){
//                if (j > 0 && nums[j] == nums[j - 1])//相等则跳过
//                    continue;
                int left = j+1, right = nums.length -1;
                while( left < right){
                    if( nums[i] + nums[j] + nums[left] + nums[right] == target){
                        result.add(new ArrayList<Integer>(Arrays.asList(nums[i], nums[j], nums[left], nums[right])));
                        left++;
                        right--;
                    }else if(nums[i] + nums[j] + nums[left] + nums[right] < target){
                        left++;
                    }else if(nums[i] + nums[j] + nums[left] + nums[right] > target){
                        right--;
                    }
                }
            }
        }
        return result;
    }

    @Test
    public void test6(){
        int x = 100;
        String s = String.valueOf(x);
        System.out.println(s.length());
        String a = "";
        for (int i = s.length()-1; i > -1; i--){
            System.out.println(s.charAt(i));
            a += s.charAt(i);
        }
        System.out.println(a);
    }

    @Test
    public void test7(){
        System.out.println(removeElement(new int[]{3,2,2,3},3));
    }
    public int removeElement(int[] nums, int val) {
        for(int i = 0, j = nums.length; i < j ; i++){
            if(nums[i] == val){
                nums[i] = nums[i+1];
                if(i > 0){
                    i--;
                }
                j--;
            }
        }
        return nums.length;
    }
}
